package com.lovecanon.jzjl.web.controller;

import com.lovecanon.jzjl.common.BtPage;
import com.lovecanon.jzjl.common.ResultData;
import com.lovecanon.jzjl.domain.TbUser;
import com.lovecanon.jzjl.web.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@Controller
@RequestMapping("userController/")
public class UserController {


    private static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;


    @RequestMapping("usermanager.html")
    public ModelAndView init(ModelAndView mv){
        mv.setViewName("user");
        return mv;
    }

    @RequestMapping("/SaveOrUpdate")
    @ResponseBody
    public ResultData save(TbUser tbUser, HttpServletRequest request) {
//        ResultData resultData = new ResultData();
//        resultData.requestError("图包已经存在,请勿重复添加");
//        TbReceive tbreceiveByFileName = userService.getTbreceiveByFileName(receive.getFileName());
//        if (tbreceiveByFileName!=null){
//            return resultData;
//        }
//        receive.setIsUse("Y");
        return  userService.saveOrUpdate(tbUser);
    }
    @RequestMapping("/getPage")
    @ResponseBody
    public BtPage getPage(HttpServletRequest request) {
        HashMap<String,Object> queryMap = new HashMap<>();
        String userName = request.getParameter("userName");
        int pageNumber =  Integer.parseInt(request.getParameter("pageNumber"));
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        queryMap.put("pageNumber",pageNumber);
        queryMap.put("pageSize",pageSize);
        if (!StringUtils.isEmpty(userName)){
            queryMap.put("userName",userName);
        }
        return userService.findAll(queryMap);
    }
}
